package cn.edu.scau.cmi.liangguan.hibernate.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scau.cmi.liangguan.database.domain.Tutor;
import cn.edu.scau.cmi.liangguan.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.liangguan.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.liangguan.hibernate.domain.Student;
import cn.edu.scau.cmi.liangguan.hibernate.domain.Teacher;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class HDBServlet
 */
@WebServlet("/HDBServlet")
public class HDBServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HDBServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/json;charset=GBK");
		
		PrintWriter out = response.getWriter();
    
		String op = request.getParameter("op");
		//System.out.println("HDBClient.doPost()"+op);
		JSONArray jsonArray = null;
		if (op.compareTo("1")==0) {

			StudentDAO studentDAO = new StudentDAO();
			@SuppressWarnings("unchecked")
			List<Student> list =  studentDAO.findAll();
			ArrayList<cn.edu.scau.cmi.liangguan.database.domain.Student> arrayList = new ArrayList<>();
			for (Student student : list) {
				cn.edu.scau.cmi.liangguan.database.domain.Student student2 = new cn.edu.scau.cmi.liangguan.database.domain.Student(student.getId().toString(), student.getName());
				if (student.getTeacher()!=null) {
				student2.setT_id(student.getTeacher().getId().toString());
				student2.setT_name(student.getTeacher().getName());
				}
				arrayList.add(student2);
			}
			jsonArray  = JSONArray.fromObject(arrayList);
			out.println(jsonArray);	
		}else {
			TeacherDAO teacherDAO = new TeacherDAO();
			@SuppressWarnings("unchecked")
			List<Teacher> list = teacherDAO.findAll();
			ArrayList<Tutor> arrayList = new ArrayList<>();
			for (Teacher teacher : list) {
				Tutor tutor = new Tutor(teacher.getId().toString(),teacher.getName());
				arrayList.add(tutor);
			}
			jsonArray = JSONArray.fromObject(arrayList);
			out.println(jsonArray);
		}
	}
	
	protected void error(PrintWriter out) {
		String error = "[{'error':'true'}]";
		JSONArray jsonArray = JSONArray.fromObject(error);
		out.println(jsonArray);			
	}

}
